package com.yuanfengerp.suppliersettlement.service.impl;

import com.yuanfengerp.publicUtil.Result;
import com.yuanfengerp.publicUtil.SnowflakeIdWorker;
import com.yuanfengerp.suppliersettlement.dao.ISupplierSettlementMapper;
import com.yuanfengerp.suppliersettlement.moudel.SupplierSettlementInfo;
import com.yuanfengerp.suppliersettlement.moudel.SupplierSettlementItemInfo;
import com.yuanfengerp.suppliersettlement.service.ISupplierSettlementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SupplierSettlementServiceImpl implements ISupplierSettlementService {
    @Autowired
    private ISupplierSettlementMapper issm;
    SnowflakeIdWorker idWorker = new SnowflakeIdWorker(9);

    @Override
    public Result<String> deleteByPrimaryKey(String supplier_settlement_ID) {
        if (supplier_settlement_ID == null)
            return Result.failure(400, "要删除的结算单不存在，请确认后再尝试删除");
        if (issm.selectSupplierSettlementByPrimaryKey(supplier_settlement_ID) == null)
            return Result.failure(400, "要删除的结算单不存在，请确认后再尝试删除");
        if (issm.selectSupplierSettlementByPrimaryKey(supplier_settlement_ID).getSupplier_state() == 1)
            return Result.failure(400, "要删除的结算单存在未结算费用，请先结算后再尝试删除");
        int i = issm.deleteByPrimaryKey(supplier_settlement_ID);
        return i > 0 ? Result.success("删除成功") : Result.failure(500, "删除失败");
    }

    @Override
    public Result<String> insertSupplierSettlement(SupplierSettlementInfo supplierSettlementInfo) {
        // 检查供应商名称是否为空
        if (supplierSettlementInfo.getSupplier_name() == null || supplierSettlementInfo.getSupplier_name().isEmpty())
            return Result.failure(400, "供应商名称不能为空，请填写后再尝试添加");

        Double value = 0.0;
        supplierSettlementInfo.setSupplier_operate_time(new Timestamp(System.currentTimeMillis()));
        supplierSettlementInfo.setSupplier_review_time(new Timestamp(System.currentTimeMillis()));
        supplierSettlementInfo.setSupplier_state(0);
        supplierSettlementInfo.setSupplier_settlement_ID(idWorker.nextId());
        int i = issm.insertSupplierSettlement(supplierSettlementInfo);
        if (i > 0) {
            // 如果插入成功，继续处理连表新增的费用项信息插入
            List<SupplierSettlementItemInfo> supplierSettlementItemInfos = supplierSettlementInfo.getSupplierSettlementItemInfoList();
            if (supplierSettlementItemInfos != null && !supplierSettlementItemInfos.isEmpty()) {
                for (SupplierSettlementItemInfo supplierSettlementItemInfo : supplierSettlementItemInfos) {
                    value = supplierSettlementItemInfo.getSupplier_expense_total_value();
                    supplierSettlementInfo.setSupplier_settlement_total_receivable(value);

                    System.out.println(supplierSettlementInfo.getSupplier_settlement_total_receivable());
                    int i1 = issm.updateSupplierSettlement(supplierSettlementInfo);
                    if (i1 <= 0)
                        return Result.failure(500, "更新费用信息失败");
                    supplierSettlementItemInfo.setSupplier_expense_ID(supplierSettlementInfo.getSupplier_settlement_ID());
                    int j = issm.insertSupplierSettlementItemInfo(supplierSettlementItemInfo);
                    if (j <= 0) {
                        // 如果插入连接表失败，需要进行相应处理，这里简单返回失败结果
                        return Result.failure(500, "添加费用项到连接表失败");
                    }
                }
            }

            supplierSettlementInfo.setSupplier_settlement_total_receivable(value);

            System.out.println(supplierSettlementInfo.getSupplier_settlement_total_receivable());
            int i1 = issm.updateSupplierSettlement(supplierSettlementInfo);
            if (i1 <= 0)
                return Result.failure(500, "更新费用信息失败");
            // 如果所有操作都成功，返回成功结果
            return Result.success("添加成功");
        } else {
            // 如果插入失败，返回失败结果并附带错误信息
            return Result.failure(500, "添加失败，数据库插入操作未成功");
        }
    }

    @Override
    public Result<String> updateSupplierSettlement(SupplierSettlementInfo supplierSettlementInfo) {
        if (supplierSettlementInfo.getSupplier_settlement_ID() == null)
            return Result.failure(400, "要修改的结算单不存在，请确认后再尝试修改");
        supplierSettlementInfo.setSupplier_review_time(new Timestamp(System.currentTimeMillis()));
        int i = issm.updateSupplierSettlement(supplierSettlementInfo);
        return i > 0 ? Result.success("修改成功") : Result.failure(500, "修改失败");
    }

    @Override
    public Result<SupplierSettlementInfo> selectSupplierSettlementByPrimaryKey(String supplier_settlement_ID) {
        if (supplier_settlement_ID == null)
            return Result.failure(400, "要查询的结算单不存在，请确认后再尝试查询");
        SupplierSettlementInfo supplierSettlementInfo = issm.selectSupplierSettlementByPrimaryKey(supplier_settlement_ID);
        if (supplierSettlementInfo == null)
            return Result.failure(400, "要查询的结算单不存在，请确认后再尝试查询");
        return Result.success(supplierSettlementInfo);
    }

    @Override
    public Result<List<SupplierSettlementInfo>> selectAllSupplierSettlement() {
        List<SupplierSettlementInfo> supplierSettlementInfos = issm.selectAllSupplierSettlement();
        if (supplierSettlementInfos.isEmpty())
            return Result.failure(400, "没有数据");
        return Result.success(supplierSettlementInfos);
    }

    @Override
    public Result<List<SupplierSettlementInfo>> selectSupplierSettlement(Map<String, Object> params) {



        System.out.println(params.get("pageSize"));
        System.out.println(params.get("pageNum"));
        params.put("pageNum", ((int) params.get("pageNum") - 1) * (int) params.get("pageSize"));
        List<SupplierSettlementInfo> supplierSettlementInfos = issm.selectSupplierSettlement(params);
        if (supplierSettlementInfos.isEmpty())
            return Result.failure(400, "没有数据");
        return Result.success(supplierSettlementInfos);
    }

    @Override
    public Result<String> batchdelete(ArrayList<String> supplier_settlement_IDs) {
        int i = issm.batchdelete(supplier_settlement_IDs);
        return i > 0 ? Result.success("删除成功") : Result.failure(500, "删除失败");
    }

    @Override
    public Result<String> batchreview(ArrayList<String> supplier_settlement_IDs) {
        for (String supplier_settlement_ID : supplier_settlement_IDs) {
            SupplierSettlementInfo supplierSettlementInfo = issm.selectSupplierSettlementByPrimaryKey(supplier_settlement_ID);
            if (supplierSettlementInfo == null)
                return Result.failure(400, "要审核的结算单不存在，请确认后再尝试审核");
            if (supplierSettlementInfo.getSupplier_state() == 1)
                return Result.failure(400, "要审核的结算单已审核，请确认后再尝试审核");
        }
        int i = issm.batchreview(supplier_settlement_IDs);
        return i > 0 ? Result.success("审核成功") : Result.failure(500, "审核失败");
    }

    @Override
    public Result<SupplierSettlementInfo> selectSupplierSettlementItemInfo(String supplier_settlement_ID) {
        SupplierSettlementInfo supplierSettlementInfo = issm.selectSupplierSettlementItemInfo(supplier_settlement_ID);
        System.out.println("supplierSettlementInfo: " + supplierSettlementInfo);
        if (supplierSettlementInfo == null)
            return Result.failure(400, "要查询的结算单不存在，请确认后再尝试查询");
        return Result.success(supplierSettlementInfo);
    }
}
